package testadventure;

import edu.clemson.cs.hamptos.adventure.AdventureCommand;
import edu.clemson.cs.hamptos.adventure.AdventureEngine;
import edu.clemson.cs.hamptos.adventure.AdventureTarget;
import edu.clemson.cs.hamptos.adventure.AdventureWindow;
import edu.clemson.cs.hamptos.adventure.DoNotUnderstandException;
import edu.clemson.cs.hamptos.adventure.VerbStrategyAlt;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

/**
 *<p></p>
 * @author Nicks
 */
public class ItemExit extends BaseItem {
    /**
     * <p>Constructs a new instance of <code>ItemExit</code>.</p>
     */
    protected ItemExit() {
        
    }
    /**
     * <p>Constructs a new instance of <code>ItemExit</code>.</p>
     * @param inBlob - of type StringBuilder
     */
    ItemExit(StringBuilder inBlob)
    {
        super(inBlob);
        
        Scanner blobScan = new Scanner(inBlob.toString());
        blobScan.useDelimiter(";\r\n");
        
        Scanner verbIOScan = new Scanner(blobScan.next());
        verbIOScan.useDelimiter(" ");
        while (verbIOScan.hasNext())
        {
            String text = verbIOScan.next();
            if (text.length() > 0)
            {
                VerbStrategyAlt vsa = TheAdventure.verbMap.get(text);
                Set<String> verbs = vsa.getVerbs();
                VContainer vc = new VContainer(vsa, "y", "n");
                Iterator<String> it = verbs.iterator();
                while(it.hasNext())
                {
                    accIOVerbs.put(it.next(), vc);
                }
            }
        }
        
        Scanner verbDOScan = new Scanner(blobScan.next());
        verbDOScan.useDelimiter(" ");
        while (verbDOScan.hasNext())
        {
            String text = verbDOScan.next();
            if (text.length() > 0)
            {
                VerbStrategyAlt vsa = TheAdventure.verbMap.get(text);
                Set<String> verbs = vsa.getVerbs();
                VContainer vc = new VContainer(vsa, "y", "n");
                Iterator<String> it = verbs.iterator();
                while(it.hasNext())
                {
                    accDOVerbs.put(it.next(), vc);
                }
            }
        }
    }
    
    protected String theExit;
   
    @Override
    public String getDescription()
    {
        if (description.equals("NSEWdescr"))
        {
            return "A way leading " + name;
        }
        return description;
    }
    
    @Override
    public String getShortDescription()
    {
        if (description.equals("NSEWdescr"))
        {
            return name;
        }
        return shortDescr;
    }
    
    protected void fillClone(ItemExit cl)
    {
        super.fillClone(cl);
        cl.theExit = theExit;
    }
    
    @Override
    public AdventureTarget clone()
    {
        ItemExit cl = new ItemExit();
        fillClone(cl);
        
        return cl;
    }
    /**
     * <p>Creates a new <code>ItemExit</code> and copies the existing fields
     * to the new instance.</p>
     * @param newName - of type String
     * @param newTarg - of type String
     * @param enabled - of type int
     * @return <code>ItemExit</code> - a copy of the original
     */
    public ItemExit clone(String newName, String newTarg, int enabled)
    {
        ItemExit ret = new ItemExit();
        fillClone(ret);
        ret.name = newName;
        ret.theExit = newTarg;
        ret.stats.put("enabled", enabled);
        ret.activatedTxt = "You can now reach the " + newName + ".";
        
        return ret;
    }
    
    @Override
    public void doCommandTo(AdventureCommand c, AdventureEngine e, 
                            AdventureWindow w) 
            throws DoNotUnderstandException {
        String verb = c.getVerb();
        if (!(checkVerbAsDO(verb)))
        {
            throw new DoNotUnderstandException(c, "You can't " + c.getVerb() 
                                               + " " + shortDescr);
        }
        //see if do can be used
        if (stats.get("enabled") == 0)
        {
            w.println(shortDescr + " can't be used right now.");
            return;
        }
        
        w.println("You go " + getShortDescription() + ".");
        e.setPlayerLocation(TheAdventure.locationMap.get(theExit));
    }
    
    @Override
    public void doCommandWith(AdventureCommand c, AdventureEngine e, 
                              AdventureWindow w) 
            throws DoNotUnderstandException {
        String verb = c.getVerb();
        if (!(checkVerbAsIO(verb)))
        {
            throw new DoNotUnderstandException(c, "You can't " + c.getVerb() 
                                               + " " + shortDescr);
        }
        //see if do can be used
        if (stats.get("enabled") == 0)
        {
            w.println(shortDescr + " can't be used right now.");
            return;
        }
        
        w.println("You go through " + getShortDescription() + ".");
        e.setPlayerLocation(TheAdventure.locationMap.get(theExit));
    }
}
